package com.situ.web.servlet;

import com.situ.web.dao.IStudentDao;
import com.situ.web.dao.impl.StudentDaoImpl;
import com.situ.web.pojo.Student;
import com.situ.web.service.IStudentService;
import com.situ.web.service.impl.StudentServiceImpl;
import com.situ.web.util.JDBCUtil;
import com.situ.web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private IStudentDao studentDao = new StudentDaoImpl();
    private IStudentService studentService = new StudentServiceImpl();



    //重写service方法,访问servlet默认访问service方法
    // http://localhost:8080/hello.jsp
    // http://localhost:8080/student
    // http://localhost:8080/student?method=selectAll
    // http://localhost:8080/student?method=deleteById&id=2
    // http://localhost:8080/student?method=add
    // http://localhost:8080/student?method=delete
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "selectAll":
                selectAll(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "toUpdate":
                toUpdate(req, resp);
                break;
            case "update":
                update(req, resp);
                break;

        }
    }
    //student?method=selectByPage&pageNo=1&pageSize=5
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        if (pageNo == null || pageNo.equals("")) {
            pageNo = "1";
        }
        if (pageSize == null || pageSize.equals("")) {
            pageSize = "5";
        }
        PageInfo pageInfo = studentService.selectByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        System.out.println(pageInfo);
        req.setAttribute("pageInfo", pageInfo);
        req.getRequestDispatcher("/student_list.jsp").forward(req, resp);

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(age), gender);
        studentService.update(student);

        resp.sendRedirect("/student");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.toUpdate");
        String id = req.getParameter("id");

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Student student = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,age,gender FROM student WHERE id=?";
            //Statement statement = connection.createStatement();
            //预编译
            statement = connection.prepareStatement(sql);
            statement.setInt(1, Integer.parseInt(id));
            //ResultSet resultSet = statement.executeQuery(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                student = new Student(Integer.parseInt(id), name, age, gender);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }

        req.setAttribute("student", student);
        req.getRequestDispatcher("/student_update.jsp").forward(req, resp);

    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.add");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        Student student = new Student(name, Integer.parseInt(age), gender);
        studentService.add(student);


        resp.sendRedirect("/student");


    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("StudentServlet.deleteById");
        String id = req.getParameter("id");
        studentService.deleteById(Integer.parseInt(id));
        //重定向到/student查找所有这个servlet
        //重定向状态码： 302
        resp.sendRedirect("/student");
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentServlet.selectAll");
        List<Student> list = studentService.selectAll();

        //把list放到request里面，理解成放到一块内存区域里面
        req.setAttribute("list", list);
        //跳转到student_list.jsp进行展示
        req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
    }


}